package com.xmb.wcapi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xmb.wcapi.constant.DepConstant;
import com.xmb.wcapi.dto.ReportDTO;
import com.xmb.wcapi.entity.BaseMaterial;
import com.xmb.wcapi.mapper.BaseMaterialMapper;
import com.xmb.wcapi.mapper.DjBaseMaterialMapper;
import com.xmb.wcapi.service.IDjBaseMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 */
@Slf4j
@Service
public class DjBaseMaterialServiceImpl extends ServiceImpl<DjBaseMaterialMapper, BaseMaterial> implements IDjBaseMaterialService {

    @Autowired
    private DjBaseMaterialMapper baseMaterialMapper;

    @Override
    public IPage<BaseMaterial> getBaseMaterialList(Integer page, Integer limit, String cusCode, String invCode, String invDef,boolean... type) {
        //查询顶级bom
        QueryWrapper<BaseMaterial> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(cusCode),"cus_code",cusCode);
        wrapper.like(StringUtils.isNotEmpty(invCode),"dj_material",invCode);
        wrapper.like(StringUtils.isNotEmpty(invDef),"type_of_machine",invDef);
        wrapper.eq("checked",true);
        if (type.length > 0){
            wrapper.notIn("class_type","A");
        }
        Page<Object> objectPage = new Page<>(page, limit);
        objectPage.setCountId("getBaseMaterialList_Count");
        Page<BaseMaterial> list = baseMaterialMapper.getBaseMaterialList(objectPage, wrapper);
        for (BaseMaterial record : list.getRecords()) {
            if (StringUtils.isEmpty(record.getBomId())){
                record.setBomId("0");
            }
        }
        return list;
    }

    public IPage<ReportDTO> getReportList(Integer page, Integer limit, String cusCode, String invCode, String invDef){
        BaseMaterial baseMaterial = null;
        if (StringUtils.isNotEmpty(invCode)){
            List<BaseMaterial> materials = baseMaterialMapper.selectList(Wrappers.lambdaQuery(BaseMaterial.class)
                    .eq(BaseMaterial::isChecked,true)
                    .like(BaseMaterial::getDjMaterial, invCode));
            if (null != materials){
                if (!materials.isEmpty()){
                    baseMaterial = materials.get(0);
                }
            }
        }
        Integer leve = null;
        String allId = null;
        if (null != baseMaterial){
            leve = baseMaterial.getLevels();
            allId = baseMaterial.getAllComponentId();
        }
        Page<Object> objectPage = new Page<>(page, limit);
        objectPage.setCountId("getReportList_COUNT");
        Page<BaseMaterial> reportList = baseMaterialMapper.getReportList(objectPage, cusCode, invCode, invDef, leve, allId);

        //查询所有bom
        List<BaseMaterial> materials = baseMaterialMapper.selectList(Wrappers.lambdaQuery(BaseMaterial.class));
        if (!materials.isEmpty()){
            //按照all分类
            Map<String, List<BaseMaterial>> collect = materials.stream().collect(Collectors.groupingBy(BaseMaterial::getAllComponentId));
            //循环拼接下一级
            for (BaseMaterial record : reportList.getRecords()) {
                if (StringUtils.isEmpty(record.getDjMaterialCopy())){
                    getNextMaterial(record, collect.get(record.getAllComponentId()),record.getBomId());
                }
            }
        }

        //料号更换 同库存管理一样
        for (BaseMaterial record : reportList.getRecords()) {
            if (StringUtils.isNotEmpty(record.getClassType())){
                //只有在辅料的时候才会 客户料号和东矩料号取反
                if (record.getClassType().equals("C") || record.getClassType().equals("H")){
                    //下阶料号
                    String djMaterialCopy = record.getDjMaterialCopy();
                    if (StringUtils.isNotEmpty(djMaterialCopy)){
                        record.setCusMaterialCopy(djMaterialCopy);
                        record.setDjMaterialCopy("");
                    }
                }
            }
            if (record.getClassTypeCopy().equals("C") || record.getClassTypeCopy().equals("H")){
                //主阶料号
                String djMaterial = record.getDjMaterial();
                if (StringUtils.isNotEmpty(djMaterial)){
                    record.setCusMaterial(djMaterial);
                    record.setDjMaterial("");
                }
            }
        }
        IPage<ReportDTO> pp = getReportDTOIPage(page, limit, reportList);
        return pp;
    }

    private void getNextMaterial(BaseMaterial record, List<BaseMaterial> materials,String bomId) {
        boolean mark = false;
        if (StringUtils.isEmpty(bomId)){
            return;
        }
        for (BaseMaterial material : materials) {
            //找到下一级
            if (bomId.equals(material.getParentBomId())){
                //不是勾选状态就递归
                if (material.isChecked()){
                    //被勾选就不用继续往下找
                    record.setClassType(material.getClassType());
                    record.setDjMaterialCopy(material.getDjMaterial());
                    record.setCusMaterialCopy(material.getCusMaterial());
                    record.setTypeOfMachine(material.getTypeOfMachine());
                    if (StringUtils.isEmpty(material.getTypeOfMachine())){
                        record.setTypeOfMachine(material.getMaterialMrk());
                    }
                    record.setBaseQty(material.getBaseQty());
                    record.setClassName(material.getClassName());
                    return;
                }else {
                    mark = true;
                    //重置bomId
                    bomId = material.getBomId();
                    break;
                }
            }
        }
        if (mark){
            getNextMaterial(record,materials,bomId);
        }
    }

    private static IPage<ReportDTO> getReportDTOIPage(Integer page, Integer limit, Page<BaseMaterial> reportList) {
        IPage<ReportDTO> pp = new Page<>(page, limit);
        //数据转换
        List<ReportDTO> list = new ArrayList<>();
        for (BaseMaterial record : reportList.getRecords()) {
            ReportDTO dto = getReportDTO(record);
            list.add(dto);
        }
        pp.setRecords(list);
        pp.setTotal(reportList.getTotal());
        pp.setCurrent(reportList.getCurrent());
        pp.setSize(reportList.getSize());
        pp.setPages(reportList.getPages());
        return pp;
    }

    private static ReportDTO getReportDTO(BaseMaterial record) {
        ReportDTO dto = new ReportDTO();
        dto.setAllComponentId(record.getAllComponentId());
        dto.setBomId(record.getBomId());
        dto.setParentId(record.getParentBomId());
        dto.setBmb01(record.getDjMaterial());
        dto.setIma021m(record.getCusMaterial());
        dto.setIma02m(record.getMaterialMrk());
        dto.setBmb02("");
        dto.setBmb03(record.getDjMaterialCopy());
        dto.setIma021(record.getCusMaterialCopy());
        dto.setIma02(record.getTypeOfMachine());
        dto.setQPA(record.getBaseQty());
        dto.setMaterialType(record.getClassType());
        dto.setMaterialName(record.getClassName());
        return dto;
    }

    @Override
    public Page<BaseMaterial> selectTableList(Integer page, Integer limit, String djMaterial) {
        Page<BaseMaterial> baseMaterialPage = baseMaterialMapper.selectTableList(new Page<>(page, limit), djMaterial);
        return baseMaterialPage;
    }
}
